/**
 * Created by cchen on 2015/6/8.
 */
'use strict';

angular.module('backofficeApp.bannerEdit.controllers', []).
    controller('bannerEditCtrl', ['$scope', '$routeParams', 'CommunityService', '$http', '$location', function ($scope, $routeParams, communityService, $http, $location) {

        var bannerId = $routeParams.bannerId;
        $scope.getDetail = function () {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/getBannerDetail/' + bannerId, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.banner = data.data.banner;
                $scope.content = data.data.content;
                $scope.bindIds = data.data.bindIds;
                $scope.bindList = data.data.bindList;
            }).error(function () {
            });
        };

        $scope.getDetail();

        $scope.deleteBind = function (objId) {
            bootbox.confirm('是否确定结束绑定？', function (result) {
                if (result) {
                    $http.delete('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/deleteBannerBind/' + objId, {
                        headers: {'Content-Type': 'application/json;charset=utf-8'}
                    }).success(function (data) {
                        $scope.getDetail();
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        };

        $scope.editImg = function () {
            $('#img').modal('show');
            $scope.message = $scope.banner.message;
        };
        $scope.bindCommunity = function () {
            $('#bind').modal('show');
        };

        $scope.selectFile = function (files) {
            $scope.image = files[0]
        };

        $scope.confirmEdit = function () {
            if ($scope.banner.type == 1 && $scope.image == null) {
                alert('请选择图片');
                return;
            }
            if ($scope.banner.type == 2 && ($scope.message == null || $scope.message == '')) {
                alert('请输入内容');
                return;
            }

            $('#img').modal('hide');
            var fd = new FormData();
            if ($scope.image != null)
                fd.append('image1', $scope.image);
            if ($scope.message != null || $scope.message != '')
                fd.append('message', $scope.message);
            fd.append('bannerId', bannerId);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/updateCommunityBanner', fd, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            }).success(function (data) {
                $scope.getDetail();
            }).error(function () {
            });
        };

        $scope.communityIds = [];
        $scope.toggle = function (ck, objId) {
            !ck ? $scope.communityIds.push(objId) : $scope.communityIds.remove(objId);
            $scope.getAllContent();
        };

        $scope.confirmBind = function () {
            $('#bind').modal('hide');
            if ($scope.communityIds.length == 0) {
                return;
            }

            var input = {
                bannerId: bannerId,
                type: $scope.banner.type,
                community: $scope.communityIds
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/bindCommunityBanner', input, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getDetail();
            }).error(function () {
            });
        }
    }]);